package com.work.studio.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.work.studio.R;
import com.work.studio.utils.DisplayUtils;


/**
 * 仿IOS对话框
 *
 * @author
 */
public class UpgradeDialog {

    private Context context;

    private Display display;

    private int screenWidth = 0;

    /**
     * 显示标题
     */
    private boolean showTitle;
    /**
     * 显示内容
     */
    private boolean showMsg;
    /**
     * 显示取消按钮
     */
    private boolean showNeBtn;
    /**
     * 显示确定按钮
     */
    private boolean showPoBtn;

    /**
     * 对话框的标题
     */
    private TextView dialogTitle;
    /***
     * 对话框的内容
     */
    private TextView dialogMsg;
    /**
     * 确定按钮
     */
    private Button poBtn;

    public Button getPoBtn() {
        return poBtn;
    }

    public Button getNeBtn() {
        return neBtn;
    }

    /**
     * 取消按钮
     */
    private Button neBtn;

    /**
     * 布局
     */
    private LinearLayout dialogLayout;

    /**
     * 按钮之间的分割线
     */
    private View line;

    private Dialog dialog;

    public UpgradeDialog(Context context) {

        this.context = context;

        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        display = manager.getDefaultDisplay();

        screenWidth = (int) (DisplayUtils.getScreenRealWidth((Activity) context)/3);

    }

    /**
     * 初始化
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    public UpgradeDialog builder() {

        /**
         * 获取布局样式
         */
        View view = LayoutInflater.from(context).inflate(R.layout.view_alert_ios_dialog, null);

        /**
         * 获取控件
         */
        dialogTitle = (TextView) view.findViewById(R.id.alert_title);
        dialogTitle.setVisibility(View.GONE);

        dialogMsg = (TextView) view.findViewById(R.id.alert_msg);
        dialogMsg.setVisibility(View.GONE);

        poBtn = (Button) view.findViewById(R.id.alert_right_btn);
        poBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
//                poBtn.setScaleType(ScaleType.CENTER);
                if (hasFocus) {
                    poBtn.setScaleX(1.3f);
                    poBtn.setScaleY(1.3f);
                } else {
                    poBtn.setScaleX(1.0f);
                    poBtn.setScaleY(1.0f);
                }
            }
        });
        poBtn.setVisibility(View.GONE);

        neBtn = (Button) view.findViewById(R.id.alert_left_btn);
        neBtn.setVisibility(View.GONE);
        neBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
//                poBtn.setScaleType(ScaleType.CENTER);
                if (hasFocus) {
                    neBtn.setScaleX(1.3f);
                    neBtn.setScaleY(1.3f);
                } else {
                    neBtn.setScaleX(1.0f);
                    neBtn.setScaleY(1.0f);
                }
            }
        });
        dialogLayout = (LinearLayout) view.findViewById(R.id.dialog_alert_layout);
//		dialogLayout.setVisibility(View.GONE);

        line = view.findViewById(R.id.btn_line);
        line.setVisibility(View.GONE);

        dialog = new Dialog(context, R.style.AlertDialogStyle);

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {

                if (listener != null) {
                    listener.onDismiss(dialogInterface);
                }
            }
        });
        dialog.setContentView(view);

        dialogLayout.setLayoutParams(new FrameLayout.LayoutParams(screenWidth, FrameLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }


    /**
     * 设置标题
     * @return
     */
    public UpgradeDialog setTitle(String title) {

        showTitle = true;

        if (title == null) {

            dialogTitle.setText("");
        } else {

            dialogTitle.setText(title);
        }

        return this;
    }

    /**
     * 设置内容
     *
     * @return
     */
    public UpgradeDialog setMsg(String msg) {

        showMsg = true;

        if (msg == null) {

            dialogMsg.setText("");
        } else {

            dialogMsg.setText(msg);
        }


        return this;
    }


    /**
     * 确定按钮
     *
     * @param poBtnName
     * @return
     */
    public UpgradeDialog setPoBtn(String poBtnName, final OnClickListener listener) {
        showPoBtn = true;
        if (poBtnName == null) {
            poBtn.setText("");
        } else {
            poBtn.setText(poBtnName);
        }
        if (listener != null) {
            poBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(v);
                    dialog.dismiss();
                }
            });
        }
        return this;
    }



    /**
     * 确定按钮
     *
     * @param poBtnName
     * @return
     */
    public UpgradeDialog setPoBtn(String poBtnName, boolean isDismiss, final OnClickListener listener) {
        showPoBtn = true;
        if (poBtnName == null) {
            poBtn.setText("");
        } else {
            poBtn.setText(poBtnName);
        }
        if (listener != null) {
            poBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(v);
                    if (isDismiss) {
                        dialog.dismiss();
                    }

                }
            });
        }
        return this;
    }

    /**
     * 确定按钮
     *
     * @param poBtnName
     * @return
     */
    public UpgradeDialog setPoBtn(String poBtnName) {
        showPoBtn = true;
        if (poBtnName == null) {
            poBtn.setText("");
        } else {
            poBtn.setText(poBtnName);
        }
        return this;
    }


    public UpgradeDialog setPoBtnClickEnable(boolean enable) {
       if (poBtn != null) {
           poBtn.setClickable(enable);
       }

        return this;
    }

    /**
     * 确定按钮
     *
     * @param poBtnName
     * @return
     */
    public UpgradeDialog setPoBtn(String poBtnName, final OnClickListener listener, final boolean noDismiss) {
        showPoBtn = true;
        if (poBtnName == null) {
            poBtn.setText("");
        } else {
            poBtn.setText(poBtnName);
        }
        if (listener != null) {
            poBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(v);
                    if (noDismiss) {

                        dialog.dismiss();
                    }
                }
            });
        }
        return this;
    }

    /**
     * 设置字体颜色
     *
     * @param color
     * @return
     */
    public UpgradeDialog setPosBtnColor(String color) {
        showPoBtn = true;
        if (poBtn != null) {
            poBtn.setTextColor(Color.parseColor(color));
        }
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param color
     * @return
     */
    public UpgradeDialog setPosBtnBackGroundColor(String color) {
        showPoBtn = true;
        if (poBtn != null) {
            poBtn.setBackgroundColor(Color.parseColor(color));
        }
        return this;
    }

    /**
     * 取消按钮
     *
     * @param neBtnName
     * @return
     */
    public UpgradeDialog setNeBtn(String neBtnName, final OnClickListener listener) {
        showNeBtn = true;
        if (neBtnName == null) {
            neBtn.setText("");
        } else {
            neBtn.setText(neBtnName);
        }
        if (listener != null) {

            neBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    listener.onClick(v);
                    dialog.dismiss();
                }
            });
        }

        return this;
    }

    /**
     * 设置字体颜色
     *
     * @param color
     * @return
     */
    public UpgradeDialog setNeBtnColor(String color) {
        showNeBtn = true;
        if (neBtn != null) {
            neBtn.setTextColor(Color.parseColor(color));
        }
        return this;
    }

    /**
     * 设置显示值
     *
     * @param name
     * @return
     */
    public UpgradeDialog setNeBtnName(String name) {
        showNeBtn = true;
        if (neBtn != null) {
            neBtn.setText(name);
        }
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param color
     * @return
     */
    public UpgradeDialog setNeBtnBackGroundColor(String color) {
        showNeBtn = true;
        if (neBtn != null) {
            neBtn.setBackgroundColor(Color.parseColor(color));
        }
        return this;
    }

    /**
     * 设置是否在屏幕之外点击退出对话框
     *
     * @param cancel
     * @return
     */
    public UpgradeDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    /**
     * 是否在触摸自己范围之外退出
     *
     * @param cancel true的时候点击退出
     *               false只能通过dismiss退出
     * @return
     */
    public UpgradeDialog setCancleAble(boolean cancel) {
        dialog.setCancelable(cancel);

        return this;
    }

    public UpgradeDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);

        return this;
    }

    /**
     * 整理布局
     */
    public void setLayout() {

        /**
         * 如果没有标题也没有内容
         * 默认加标题
         */
        if (!showTitle && !showMsg) {

            dialogTitle.setVisibility(View.VISIBLE);
            dialogTitle.setText("标题");
        }

        if (showTitle) {
            dialogTitle.setVisibility(View.VISIBLE);
        }

        if (showMsg) {
            dialogMsg.setVisibility(View.VISIBLE);
        }

        /**
         * 如果没有确定和取消按钮
         * 默认确定按钮
         */
        if (!showPoBtn && !showNeBtn) {

            poBtn.setText("确定");
            poBtn.setVisibility(View.VISIBLE);
            poBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);

            poBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if (dialog != null) {

                        dialog.dismiss();
                    }
                }
            });
        }

        if (showPoBtn && !showNeBtn) {
            poBtn.setVisibility(View.VISIBLE);
            poBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);
        }

        if (!showPoBtn && showNeBtn) {

            neBtn.setVisibility(View.VISIBLE);
            neBtn.setBackgroundResource(R.drawable.alertdialog_single_selector);
        }

        if (showNeBtn && showPoBtn) {

            poBtn.setVisibility(View.VISIBLE);
            poBtn.setBackgroundResource(R.drawable.alertdialog_right_selector);
            line.setVisibility(View.VISIBLE);
            neBtn.setVisibility(View.VISIBLE);
            neBtn.setBackgroundResource(R.drawable.alertdialog_right_selector);
        }
    }

    public void show() {

        setLayout();

        if (dialog != null) {

            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing())
            dialog.cancel();
    }


    public boolean isShow() {

        if (dialog != null && dialog.isShowing()) {
            return true;
        }
        return false;
    }

    private IOSDismissListener listener;

    public void setPoBtnClick(boolean isClick) {

        if (poBtn != null) {
            poBtn.setClickable(isClick);
        }
    }

    public void setType() {
        if (dialog != null) {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
    }

    public interface IOSDismissListener {
        public void onDismiss(DialogInterface listener);
    }

    public void setOnDismissListener(IOSDismissListener listener) {

        this.listener = listener;
    }
}
